home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1998 October / EnigmA AMIGA RUN 31 (1998)(G.R. Edizioni)(IT)[!][issue 1998-10].iso / earkit / chat / amarquee / edittextfile.rexx < prev    next >
OS/2 REXX Batch file  |  1998-09-22  |  4KB  |  141 lines

  1. /* 
  2.    EditTextFile.rexx v1.1 by Jeremy Friesner
  3.  
  4.    An ARexx script to cleanly modify text config
  5.    lines.  Any line beginning with sReplaceMe in 
  6.    the File fModifyMe will be replaced with the line sWithMe.
  7.    
  8.    If no such thing as sReplaceMe is found in the
  9.    file, then sWithMe will be added to the end of
  10.    the file. 
  11.  
  12.    This script should never lose the original file:
  13.    the first thing it does it rename the file to
  14.    filename.bak and then reconstructs the modified
  15.    version at the original file name by reading the
  16.    .bak file.  If an error occurs, it will restore
  17.    filename.bak back to the original filename.
  18.    
  19.    Any ^ signs in the sReplaceMe or sWithMe args will
  20.    be turned into space characters.  (Although sReplaceMe
  21.    doesn't actually need these, because it's the last
  22.    arg and thus spaces will be included in it anyway.
  23.    Isn't Rexx argument passing fun?)
  24. */
  25.    
  26. parse arg fModifyMe sReplaceMe sWithMe
  27.  
  28. address COMMAND
  29.  
  30. sReplaceMe = ChangeToSpaces(sReplaceMe)
  31. sWithMe    = strip(ChangeToSpaces(sWithMe))
  32.  
  33. fBackupFile = fModifyMe || ".bak"
  34. say "File to be modified   = [" || fModifyMe || "]"
  35. say "Backup file           = [" || fBackupFile || "]"
  36. say "String to add/replace = [" || sReplaceMe || "]"
  37. say "add/Replace with      = [" || sWithMe || "]"
  38.  
  39. keylength = length(sReplaceMe)
  40.  
  41. /* First, make sure rexxsupport.library is available. */
  42. call addlib("rexxsupport.library", 0, -30, 0)
  43.  
  44. /* Delete any .bak file that now exists */
  45. call delete(fBackupFile)
  46.  
  47. /* Rename the original filename to filename.bak */
  48. if (rename(fModifyMe, fBackupFile) == 0) then do
  49.     say "Error:  Couldn't rename " || fModifyMe || " to " || fBackupFile
  50.     exit(30)
  51.     end
  52. BFileRenamed = 1
  53.     
  54. /* Default == haven't found the line we want to replace yet */
  55. BFoundOurLine = 0
  56.  
  57. /* Open the backup file for reading */
  58. if (open('oldfile',fBackupFile,'R') == 0) then do
  59.     say "Couldn't open backup file " || fBackupFile
  60.     call error
  61.     end
  62.     
  63. /* and the new file for writing */
  64. if (open('newfile',fModifyMe,'W') == 0) then do
  65.     say "Couldn't open output file " || fModifyMe
  66.     call error
  67.     end
  68.  
  69.  
  70. /* Scan the old file, writing the new one */
  71. do while (EOF('oldfile') == 0)
  72.  nextline = readln('oldfile')
  73.  sCheckPart = left(nextline,keylength)
  74.  
  75.  if ((BFoundOurLine == 0)&(sCheckPart = sReplaceMe)) then 
  76.  do
  77.      /* don't write out this line... write out our substitute instead! */
  78.      say "Found: [" || nextline || "] replacing with [" || sWithMe || "]"
  79.     call writeln('newfile',sWithMe)
  80.     BFoundOurLine = 1
  81.  end
  82.  else do
  83.     call writeln('newfile',nextline)
  84.     end
  85. end
  86.  
  87. /* If we never found our line to replace, then just add our
  88.    line at the end of the file. */
  89. if (BFoundOurLine == 0) then call writeln('newfile',sWithMe)
  90.  
  91. call close('oldfile')
  92. call close('newfile')
  93.  
  94. /* success! */
  95. successMessage = "File " || fModifyMe || " successfully modified (" || sReplaceMe || ") -> " || "(" || sWithMe || ")."
  96.  
  97. say successMessage
  98.  
  99. /* Show calling app that we succeeded */
  100. address COMMAND 'echo ' || successMessage || ' >t:edit_text_succeeded'
  101. exit(0)
  102.  
  103.  
  104. /* Changes all occurrences of the character '^' to spaces in the string. 
  105.    Needed to get around ARexx's lame argument parsing.   I think.
  106. */
  107. ChangeToSpaces: procedure
  108.     parse arg sOrig
  109.     
  110.     sNew = ""
  111.     do while (length(sOrig) > 0)
  112.         cChar = left(sOrig,1)
  113.         if (cChar == "^") then cChar = " "
  114.         sNew = sNew || cChar
  115.         sOrig = right(sOrig,length(sOrig)-1)
  116.         end
  117.     return sNew
  118.         
  119.         
  120. /* Replaces original file if an error occurs */        
  121. error:
  122.     say "An error occured!"
  123.     call close('oldfile')
  124.     call close('newfile')
  125.     if (BFileRenamed == 1) then do
  126.         say "Attempting to restore the original file " || fModifyMe
  127.  
  128.         /* Delete the incomplete file that now exists */
  129.         call rename(fModifyMe, fModifyMe || ".tobedeleted")
  130.  
  131.         /* Rename filename.bak back to filename */
  132.         if (rename(fBackupFile, fModifyMe) == 0) then do
  133.             call rename(fModifyMe || ".tobedeleted", fModifyMe)
  134.             say "Error:  Couldn't rename " || fBackupFile || " to " || fModifyMe || ", aborting now."
  135.             exit(30)
  136.             end
  137.             
  138.         call delete(fModifyMe||".tobedeleted")
  139.         say "Recovery of original file " || fModifyMe || " was successful."
  140.         end
  141.     exit(30)